<template>
  <div class="container">
    <div class="form-register">
      <div class="form-title">
        <div class="title1">
          欢迎
        </div>
        <div class="title2">
          注册
        </div>
      </div>
      <div class="form-group">
        <label for="username" class="input-title">用户名:</label>
        <input type="text" class="form-control" v-model="username" id="username" placeholder="Enter username">
      </div>
      <div class="form-group">
        <label for="pwd" class="input-title">密码:</label>
        <input type="password" class="form-control" v-model="password1" id="pwd" placeholder="Enter password">
      </div>
      <div class="form-group">
        <label for="pwd1" class="input-title">再次输入密码:</label>
        <input type="password" class="form-control" v-model="password2" id="pwd1" placeholder="Enter password again">
      </div>
      <button @click="register" class="btn btn-info btn-login">注册</button>
      <button @click="goLogin" class="btn btn-success btn-register">现在去登陆</button>
    </div>
  </div>
</template>

<script>
export default {
  name: 'registerPage',
  data () {
    return {
      username: '',
      password1: '',
      password2: ''
    }
  },
  methods: {
    register () {
      if (this.username === '' || this.password1 === '' || this.password2 === '') {
        alert('用户名或密码未输入!')
      } else if (this.password1 !== this.password2) {
        alert('两次输入密码不一致!')
      } else {
        this.$http.post('/java_kunpeng/register', {
          username: this.username,
          password1: this.password1,
          password2: this.password2
        })
          .then((response) => {
            const state = response.data.state
            const msg = response.data.msg
            if (state === '1') {
              alert('两次输入密码不一致!' + msg)
            } else if (state === '2') {
              alert('用户名已存在!' + msg)
            } else {
              alert('注册成功!' + msg)
              this.$router.push('/loginPage')
            }
          })
      }
    },
    goLogin () {
      this.$router.push('/loginPage')
    }
  }
}
</script>

<style scoped>
  .form-register {
    width: 60%;
    margin: 40px auto;
  }
  .form-title {
    font-size: 35px;
    font-weight: bolder;
    text-align: center;
    margin-bottom: 30px;
  }
  .title1 {
    display: inline;
  }
  .title2 {
    color: cadetblue;
    display: inline;
  }
  .input-title{
    font-size: 20px;
    font-weight: bold;
  }
  .btn-login {
    width: 43%;
    margin-top: 25px;
  }
  .btn-register {
    width: 43%;
    margin-left: 12%;
    margin-top: 25px;
  }
</style>
